package com.hongxun.design.utils.fileutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hongxun.design.MyApp;
import com.hongxun.design.bean.BackPhotosBean;
import com.hongxun.design.bean.BeforeExpItemBean;
import com.hongxun.design.bean.ChannelAListBean;
import com.hongxun.design.bean.Exploration;
import com.hongxun.design.bean.PhotoPassagewayItemBean;
import com.hongxun.design.bean.PointSumBean;
import com.hongxun.design.bean.PointSumIosBean;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.bean.SearchProjectBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.jbean.JChannelBean;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.FragmentUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.projectdatautil.DataFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/8/19.
 */
public class SetFileUtil {

    /**
     * 保存项目列表数据*
     */
    public static void setProjectStageListFile(List<ProjectItemBean> data) {
//        List<ProjectToIOSBean> iosBeans = new ArrayList<>();
//         for (ProjectItemBean p : data) {
//            ProjectToIOSBean i = new ProjectToIOSBean();
//            i.setBean(p);
//            iosBeans.add(i);
//        }
//        String s = "{\"projectList\":" + JSON.toJSONString(iosBeans) + "}";
        //DisableCircularReferenceDetect：关闭循环引用，防止出现$ref":"$[0]
        String s = "{\"projectList\":" + JSON.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect) + "}";
        MyLog.L(MString.SUCCESSED + "  s========== " + s);
        MyApp.File.SaveFileData(MString.ProjectStageListFile + addEnd(), s, "n1");//存储
//        if (!s.contains("$ref")){
//            MyApp.File.SaveFileData(MString.ProjectStageListFile + addEnd(), s, "n1");//存储
//        }
    }

    /**
     * 更新项目列表数据*
     */
    public static void updateProjectStageListFile(List<ProjectItemBean> data) {

        setProjectStageListFile(DataFactory.GetNewProject(data, GetFileUtil.getProjectStageListOffLineFile()));
//        setProjectStageListFile(DataFactory.GetNewProject(data, GetFileUtil.getProjectStageListFile()));
        //DataFactory.setUpdate(true);//设置更新标识
    }

    /**
     * 删除项目列表数据(仅当在线删除离线创建的项目时)*
     */
    public static void delectProjectStageListFile(ProjectItemBean data) {
        List<ProjectItemBean> cache = GetFileUtil.getProjectStageListFile();
        List<SearchProjectBean> mapcache = GetFileUtil.getMapProjectFile();
        for (ProjectItemBean p : cache) {
            if (p.getUUID() != null && p.getUUID().length() > 0 && p.getUUID().equalsIgnoreCase(data.getUUID())) {
                cache.remove(p);
                break;
            }
        }
        for (SearchProjectBean p : mapcache) {
            if (p.getUUID() != null && p.getUUID().length() > 0 && p.getUUID().equals(data.getUUID())) {
                mapcache.remove(p);
                break;
            }
        }
        setProjectStageListFile(cache);
        setMapProjectFile(mapcache);
        //DataFactory.setUpdate(true);//设置更新标识
    }

    /**
     * 删除项目(仅删除有离线缓存项目)*
     */
    public static void delectProjectFile(ProjectItemBean data) {
        try {
            List<ProjectItemBean> cache = GetFileUtil.getProjectStageListFile();
            List<SearchProjectBean> mapcache = GetFileUtil.getMapProjectFile();
            for (ProjectItemBean p : cache) {
                if (p.getUUID() != null && p.getUUID().length() > 0 && p.getUUID().equalsIgnoreCase(data.getUUID())) {
                    cache.remove(p);
                    break;
                } else if (p.getStage_id() != null && p.getStage_id().length() > 0 && p.getStage_id().equalsIgnoreCase(data.getStage_id())) {
                    cache.remove(p);
                    break;
                }
            }
            for (SearchProjectBean p : mapcache) {
                if (p.getUUID() != null && p.getUUID().length() > 0 && p.getUUID().equals(data.getUUID())) {
                    mapcache.remove(p);
                    break;
                } else if (p.getStage_ids() != null && p.getStage_ids().length() > 0 && (p.getStage_ids().contains(data.getStage_id() + ",") || p.getStage_ids().contains("," + data.getStage_id()))) {
                    mapcache.remove(p);
                    break;
                }
            }
            setProjectStageListFile(cache);
            setMapProjectFile(mapcache);//缓存
            //删除文件夹
            String path1 = MyApp.activity.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + MString.Project_ + data.getStage_id();
            String path2 = MyApp.activity.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + MString.Project_ + data.getUUID();
            FileUtil.DeleteFolder(path1);
            FileUtil.DeleteFolder(path2);

            ToastShow.showShort(MyApp.activity, "删除成功！");
        } catch (Exception e) {

            ToastShow.showShort(MyApp.activity, "删除出错！");
        }
        //DataFactory.setUpdate(true);//设置更新标识

    }

    /**
     * 删除项目缓存*
     */
    public static void delectProjectCacheFile(ProjectItemBean data) {

        try {
            List<ProjectItemBean> cache = GetFileUtil.getProjectStageListFile();
            List<SearchProjectBean> mapcache = GetFileUtil.getMapProjectFile();
            for (ProjectItemBean p : cache) {
                if (p.getUUID() != null && p.getUUID().length() > 0 && p.getUUID().equalsIgnoreCase(data.getUUID())) {
                    cache.remove(p);
                    break;
                } else if (p.getStage_id() != null && p.getStage_id().length() > 0 && p.getStage_id().equalsIgnoreCase(data.getStage_id())) {
                    cache.remove(p);
                    break;
                }
            }
            for (SearchProjectBean p : mapcache) {
                if (p.getUUID() != null && p.getUUID().length() > 0 && p.getUUID().equals(data.getUUID())) {
                    mapcache.remove(p);
                    break;
                } else if (p.getStage_ids() != null && p.getStage_ids().length() > 0 && (p.getStage_ids().contains(data.getStage_id() + ",") || p.getStage_ids().contains("," + data.getStage_id()))) {
                    mapcache.remove(p);
                    break;
                }
            }
            setProjectStageListFile(cache);
            setMapProjectFile(mapcache);//缓存
            //删除文件夹
            String path1 = MyApp.activity.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + MString.Project_ + data.getStage_id();
            String path2 = MyApp.activity.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + MString.Project_ + data.getUUID();
            FileUtil.DeleteFolder(path1);
            FileUtil.DeleteFolder(path2);
            //删除缓存文件夹
            String path3 = MyApp.activity.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + MString.Request + File.separator + MString.Project_ + data.getStage_id();
            String path4 = MyApp.activity.getFilesDir().getPath() + File.separator + MyApp.openIdent + File.separator + MString.Request + File.separator + MString.Project_ + data.getUUID();
            FileUtil.DeleteFolder(path3);
            FileUtil.DeleteFolder(path4);
            FragmentUtil.getIntence(MyApp.activity).projectFragment.onRefresh();

        } catch (Exception e) {

            ToastShow.showShort(MyApp.activity, "删除出错！");
        }

    }

    /**
     * 创建项目*
     */
    public static void createProjectStageListFile(String info) {
        List<ProjectItemBean> cache = GetFileUtil.getProjectStageListFile();
        cache.add(0, MyApp.currProject);
        addProjectStage(MyApp.currProject);//更新项目地图数据
        setProjectStageListFile(DataFactory.GetNewProject(cache, GetFileUtil.getProjectStageListFile()));//更新列表数据

        /**创建项目文件夹**/
        String path = MString.Project_ + CheckStageId_type();
        MyApp.File.createFile(path);
        /**同步文件夹**/
        MyApp.File.createFile(MString.Request + File.separator + path);//同步文件夹
        //DataFactory.setUpdate(true);//设置更新标识

        //setProjectInfoLine(info);

    }

    public static void addProjectStage(ProjectItemBean b) {
        List<SearchProjectBean> cmap = GetFileUtil.getMapProjectFile();
        SearchProjectBean bean = new SearchProjectBean();
        bean.setBean(b);
        cmap.add(0, bean);
        SetFileUtil.setMapProjectFile(DataFactory.GetProject(cmap, GetFileUtil.getMapProjectFile())); //存储
    }

    /**
     * 修改项目*
     */

    public static void updateProjectStageListFile() {
        List<ProjectItemBean> cache = GetFileUtil.getProjectStageListFile();
        for (ProjectItemBean p : cache) {
            if (p.getUUID().length() > 0 && !p.getUUID().equals(MString.code_zero) && p.getUUID().equalsIgnoreCase(MyApp.currProject.getUUID())) {
                p.setBean(MyApp.currProject);
                break;
            } else if (p.getStage_id().length() > 0 && !p.getStage_id().equals(MString.code_zero) && p.getStage_id().equals(MyApp.currProject.getStage_id())) {
                p.setBean(MyApp.currProject);
                break;
            }
        }
        addProjectStage(MyApp.currProject);//更新项目地图数据
        setProjectStageListFile(cache);//更新列表数据
    }

    /**
     * 推送修改项目阶段信息
     **/
    public static void updateProjectSchedule(String stage_id, int schedule) {
        List<ProjectItemBean> cache = GetFileUtil.getProjectStageListFile();
        for (ProjectItemBean p : cache) {
            if (p != null && p.getStage_id() != null && p.getStage_id().length() > 0 && p.getStage_id().equals(stage_id)) {
                p.setCurrent_schedule(schedule + "");
            }
        }
        setProjectStageListFile(cache);//更新列表数据

        ProjectItemBean bean = GetFileUtil.getProjectItemBeanFromCache(stage_id);//更新项目详情
        if (bean != null) {
            bean.setCurrent_schedule(schedule + "");
            SetFileUtil.setProjectInfo(JSON.toJSONString(bean), stage_id);
        }


        ProjectItemBean cacheBean = GetFileUtil.getProjectItemBeanFromRequestCache(stage_id);//更新项目上传文件详情
        if (cacheBean != null) {
            cacheBean.setCurrent_schedule(schedule + "");
            SetFileUtil.SaveFileRequestData(JSON.toJSONString(cacheBean), stage_id);
        }
    }

    /**
     * 保存项目地图数据*
     */
    public static void setMapProjectFile(List<SearchProjectBean> data) {

        MyApp.File.SaveFileData(MString.ProjectListFile + addEnd(), "{\"projectList\":" + JSON.toJSONString(data) + "}", "n1");//存储
    }

    /**
     * 删除项目阶段*
     */
    public static void delectStageMapProjectFile(ProjectItemBean bean) {
        List<SearchProjectBean> cache = GetFileUtil.getMapProjectFile();
        for (SearchProjectBean s : cache) {
            if (s.getProject_id().equals("0") && s.getUUID().equals(bean.getUUID())) {
                if (s.getStage_ids().equals(bean.getStage_id())) {
                    cache.remove(s);
                } else {
                    s.setDelBean(bean);
                }
                break;
            } else if (!s.getProject_id().equals("0") && s.getProject_id().equals(bean.getProject_id())) {
                s.setDelFlagBean(bean);
                break;
            }
        }
        setMapProjectFile(cache);

        // MyApp.File.SaveFileData(MString.ProjectListFile+addEnd(), "{\"projectList\":" + JSON.toJSONString(cache) + "}", "n1");//存储
    }

    /**
     * 保存项目1级界面获取统计数据*
     */
    public static void setPointSumFile(PointSumBean data) {
        PointSumIosBean bean = new PointSumIosBean();
        bean.setBean(data);
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageTotalInfo + "_" + CheckStageId() + addEnd(), JSON.toJSONString(bean), "no");//存储
    }

    /**
     * 保存项目1级界面获取统计数据*
     */
    public static void setPointSumFile(String data) {
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageTotalInfo + "_" + CheckStageId() + addEnd(), data, "no");//存储
    }


    /**
     * 保存项目1级界面台区数据*
     */
    public static void setTransformerLis(List<Exploration> data) {

//        List<ExplorationIos> ioses = new ArrayList<>();
//        for (Exploration e : data) {
//            ExplorationIos ios = new ExplorationIos();
//            ios.setBean(e);
//            ioses.add(ios);
//        }
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectTransformerList + "_" + CheckStageId() + addEnd(), "{\"explorationList\":" + JSON.toJSONString(data) + "}", "n1");//存储
    }

    /**
     * 保存项目1级界面台区数据*
     */
    public static void setTransformerLis(String data) {
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectTransformerList + "_" + CheckStageId() + addEnd(), data, "no");//存储
    }

    public static void setTransformerLis(Exploration data, Map<String, Object> map) {
        List<Exploration> list = GetFileUtil.getTransformerLis();
        if (list == null) {
            list = new ArrayList<>();
        } else {
            for (Exploration d : list) {
                if (!data.getExploration_id().equals(MString.code_null) && data.getExploration_id().equals(d.getExploration_id())) {
                    list.remove(d);
                    break;
                } else if (data.getExploration_id().equals(MString.code_null) && data.getUuid().equals(d.getUuid())) {
                    list.remove(d);
                    break;
                }
            }
        }
        if (data.getUuid().length() == 0 && data.getExploration_id().equals(MString.code_null))
            return;
        Exploration e = new Exploration();
        e.setBean(data);
        e.setExploration_id(data.getExploration_id());
        e.setName(JSON.parseObject(map.get(MString.pt) + "").getString(MString.name));
        e.setNumber(data.getNumber());
        e.setUuid(data.getUuid());
        e.setPoint_type(data.getPoint_type());
        e.setExploration_type(data.getPoint_type());
        list.add(e);
        setTransformerLis(list);
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void delectTransformerLis(Exploration data) {
        if (!data.getPt_id().equals("1000012")) return;
        List<Exploration> list = GetFileUtil.getTransformerLis();
        if (list == null) {
            list = new ArrayList<>();
        } else {
            for (Exploration d : list) {
                if (!data.getExploration_id().equals(MString.code_null) && data.getExploration_id().equals(d.getExploration_id())) {
                    d.setFlag(MString.del);
                    break;
                } else if (data.getExploration_id().equals(MString.code_null) && data.getUuid().equals(d.getUuid())) {
                    list.remove(d);
                    break;
                }
            }
        }

        setTransformerLis(list);
        //DataFactory.setUpdate(true);//设置更新标识
    }


    /**
     * 保存项目1级界面踏勘点列表数据*
     */
    public static void setExplorationList(String data, String type) {
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageExplorePointList + "_" + CheckStageId() + "_" + type + addEnd(), data, "n1");//存储
    }

    public static void setExplorationList(List<Exploration> data, String type) {
        String s = "{\"explorationList\":" + JSON.toJSONString(data) + "}";
        setExplorationList(s, type);
        //DataFactory.setUpdate(true);//设置更新标识
        //  MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageExplorePointList + "_" + CheckStageId() + "_" + type + addEnd(), s, "n1");//存储
    }

    public static void setExplorationList(PhotoPassagewayItemBean data, String type) {
        BeforeExpItemBean bean = FastJsonUtil.JsonToGetExploration_Id(GetFileUtil.getBeforeExp(MyApp.currExploration));
        List<Exploration> list = GetFileUtil.getExplorationList(type);
        Map<String, SystemTypeBean> map = DataUtil.getMapNumberData();
        SystemTypeBean b = map.get(data.getPt_id());
        String NUMBER = checkNumBer(b.name, list.size(), list);
        data.setNumber(NUMBER);
        Exploration exploration = new Exploration();
        exploration.setPhotoPassagewayItemBean(data, bean);
        exploration.setNumber(NUMBER);
        exploration.setPoint_type(type);
        exploration.setPt_type(Constant.TEZHENGPOINT);
        exploration.setCreate_time(System.currentTimeMillis() + "");
        list.add(exploration);
        String s = "{\"explorationList\":" + JSON.toJSONString(list) + "}";
        setExplorationList(s, type);
        //DataFactory.setUpdate(true);//设置更新标识
        // MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageExplorePointList + "_" + CheckStageId() + "_" + type + addEnd(), s, "n1");//存储

    }

    public static void setExplorationList(Exploration exploration, String type) {
        List<Exploration> list = GetFileUtil.getExplorationList(type);

        for (Exploration e : list) {
            if (e.getExploration_id().equals(exploration.getExploration_id()) && e.getUuid().equals(exploration.getUuid())) {
                e.setBeanToHas(exploration);
            }
        }
        String s = "{\"explorationList\":" + JSON.toJSONString(list) + "}";
        setExplorationList(s, type);
        //DataFactory.setUpdate(true);//设置更新标识
        // MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageExplorePointList + "_" + CheckStageId() + "_" + type + addEnd(), s, "n1");//存储

    }

    public static void delectExplorationList(String id, String type) {
        List<Exploration> list = GetFileUtil.getExplorationList(type);
        if (list != null) {
            for (Exploration e : list) {
                if ((!e.getExploration_id().equals(MString.code_null) && e.getExploration_id().equalsIgnoreCase(id)) || e.getUuid().equalsIgnoreCase(id)) {
                    if (!e.getExploration_id().equals(MString.code_null)) {
                        e.setFlag(MString.del);
                    } else {
                        list.remove(e);
                    }
                    break;
                }
            }
            String s = "{\"explorationList\":" + JSON.toJSONString(list) + "}";
            setExplorationList(s, type);
            //DataFactory.setUpdate(true);//设置更新标识
            //MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageExplorePointList + "_" + CheckStageId() + "_" + type + addEnd(), s, "no");//存储
        }
    }

    public static void delectExplorationList(Exploration l) {
        String type = l.getPoint_type();
        if (type == null || type.length() == 0) type = "11";
        List<Exploration> list = GetFileUtil.getExplorationList(type);
        if (list != null) {
            Exploration curr = null;
            for (Exploration e : list) {
                if ((!e.getExploration_id().equals(MString.code_null) && e.getExploration_id().equalsIgnoreCase(l.getExploration_id())) || (e.getExploration_id().equals(MString.code_null) && e.getUuid().equalsIgnoreCase(l.getUuid()))) {
                    if (!e.getExploration_id().equals(MString.code_null)) {
                        e.setFlag(MString.del);
                    } else {
                        curr = e;
                    }
                } else if (e.getBefore_exp().equals(MString.getValueId(l.getExploration_id(), l.getUuid()))) {
                    e.setBefore_lat("");
                    e.setBefore_exp("");
                    e.setBefore_lng("");
                    e.setCorner("");
                    e.setDistance("");
                    //删除交叉跨越
                    delectBefore(e);
                    //更新基本信息
                    UpdateInfo(e);
                    //删除上联
                }
            }
            if (curr != null) {
                list.remove(curr);
            }

            saveDelectList(list, type, l);
            //MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageExplorePointList + "_" + CheckStageId() + "_" + type + addEnd(), s, "no");//存储
        }
    }

    /**
     * 更新点位基本信息
     **/
    private static void UpdateInfo(Exploration e) {
        String ExplorationId = MString.getValueId(e.getExploration_id(), e.getUuid());
        String s = GetFileUtil.getExplorationInfo(e);
        JSONObject jsonObject = JSONObject.parseObject(s);
        jsonObject.put(MString.before_exp, "");
        jsonObject.put(MString.before_lng, "");
        jsonObject.put(MString.before_lat, "");
        jsonObject.put(MString.corner, "");
        jsonObject.put(MString.distance, "");
        setExplorationInfo(JSON.toJSONString(jsonObject), ExplorationId);
    }

    private static void saveDelectList(List<Exploration> list, String type, Exploration l) {
        String s = "{\"explorationList\":" + JSON.toJSONString(list) + "}";
        setExplorationList(s, type);
        //DataFactory.setUpdate(true);//设置更新标识
        delectTransformerLis(l);
        delectExploration(l);
        MyApp.activity.onBackPressed();
    }

    private static String checkNumBer(String head, int number, List<Exploration> list) {
        String num = head + "-" + number;
        for (Exploration b : list) {
            if (b.getNumber().equals(num)) {//过滤特征点
                return checkNumBer(head, number + 1, list);
            }
        }
        return num;
    }

    /**
     * 踏勘点照片列表*
     */
    public static void setExplorationPhoto(BackPhotosBean bean) {
        setExplorationPhoto(JSON.toJSONString(bean));
    }

    public static void setExplorationPhoto(String bean) {
        // ProjectExplorePointInfoPhoto
        //DataFactory.setUpdate(true);//设置更新标识
        MyApp.File.SaveFileDataAll(CheckStageId_type(), MString.ProjectExplorePointInfoPhoto + "_" + CheckExplorationId() + addEnd(), bean, "n1");//存储
    }

    public static void setExplorationPhotoOld(String bean) {
        // ProjectExplorePointInfoPhoto
        //DataFactory.setUpdate(true);//设置更新标识
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExplorePointInfoPhoto + "_" + CheckExplorationId() + addEnd(), bean, "n1");//存储
    }
//    public static void setExplorationrPhoto(BackPhotosBean bean) {
//        setExplorationrPhoto(JSON.toJSONString(bean));
//
//    }
//
//    public static void setExplorationrPhoto(String bean) {
//        // ProjectExplorePointInfoPhoto
//        MyApp.File.SaveFileRequestData(MString.Request + File.separator + MString.Project_ +CheckStageId_type(), MString.ProjectExplorePointInfoPhoto + "_" + CheckExplorationId() + addEnd(), bean, "no");//存储
//    }

    /**
     * 踏勘点环境信息*
     */
    public static void setEnvInfo(Map<String, Object> data) {
        String s = "{\"envInfo\":" + JSON.toJSONString(data) + "}";
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExploreEnvInfo + "_" + CheckExplorationId() + addEnd(), s, "no");//存储
        //DataFactory.setUpdate(true);//设置更新标识
        // MyApp.File.SaveFileData(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + CheckExplorationId(), MString.ProjectExploreEnvInfo + "_" + CheckExplorationId(), s, "no");//存储
    }

    public static void setEnvRInfo(Map<String, Object> data) {
        String s = "{\"envInfo\":" + JSON.toJSONString(data) + "}";
        MyLog.L(MString.SUCCESSED + " setEnvRInfo:s=" + s);
        MyApp.File.SaveFileRequestData(MString.Request + File.separator + MString.Project_ + CheckStageId_type(), MString.ProjectExploreEnvInfo + "_" + CheckExplorationId() + addEnd(), s, "n1");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    /**
     * 踏勘点跨越物上联信息*
     */
    public static void setBeforeExp(Exploration data) {
        if (data == null) return;
//        String e= GetFileUtil.getExplorationInfo(data);
//        String s = "{\"afterExp\":" + e + "}";
//        MyLog.L(MString.SUCCESSED + " setBeforeExp:s=" + s);
//        setBeforeExp(s);

        BeforeExpItemBean b = new BeforeExpItemBean();
        b.setDistance(MyApp.currExploration.getDistance());
        b.setNumber(data.getNumber());
        b.setExploration_id(MString.getValueId(MyApp.currExploration.getExploration_id(), MyApp.currExploration.getUuid()));
        b.setBefore_exp_id(MString.getValueId(data.getExploration_id(), data.getUuid()));
        b.setLat(data.getLat());
        b.setLng(data.getLng());
        String json = "{\"afterExp\":" + JSON.toJSONString(b) + "}";
        setBeforeExp(json);
        // setBeforeRequestExp(json);
        //  MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExploreAcrossBeforePoint + "_" + CheckExplorationId() + addEnd(), s, "n1");//存储
    }

    public static void setBeforeExp(String data) {
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExploreAcrossBeforePoint + "_" + CheckExplorationId() + addEnd(), data, "n1");//存储

    }

    public static void setBeforeRequestExp(String data) {
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExploreAcrossBeforePoint + "_" + CheckExplorationId() + addEnd(), data, "n1");//存储

    }

    public static void delectBefore(Exploration e) {
        //保存基本点位信息
        //MyApp.File.SaveFileData(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId,"", "no");//存储
        String ExplorationId = MString.getValueId(e.getExploration_id(), e.getUuid());
        MyApp.File.delect(CheckStageId_type(), MString.ProjectExploreAcrossBeforePoint + "_" + ExplorationId + addEnd());
        MyApp.File.delectr(CheckStageId_type(), MString.ProjectExploreAcrossBeforePoint + "_" + ExplorationId + addEnd());

        //同步
        //DataFactory.setUpdate(true);//设置更新标识
    }

    /**
     * 踏勘点通道特征点（无上级）*
     */
    public static void setChannelList(String data) {
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExploreAcrossOnChannelList + "_" + CheckExplorationId() + addEnd(), data, "no");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void setChannelRequestList(String data) {

        MyApp.File.SaveFileRequestData(MString.Request + File.separator + MString.Project_ + CheckStageId_type(), MString.ProjectExploreAcrossOnChannelList + "_" + CheckExplorationId() + addEnd(), data, "no");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void setChannelList(PhotoPassagewayItemBean data) {
        /**本地缓存**/
        MyLog.L(MString.SUCCESSED + " data==" + JSON.toJSONString(data));
        String s = GetFileUtil.getChannelList(MyApp.currExploration);
        List<ChannelAListBean> beans = null;
        if (FastJsonUtil.JsonToCheck(s)) {
            beans = FastJsonUtil.JsonToGetChannelAList(s);
            if (beans == null) beans = new ArrayList<>();
            ChannelAListBean c = new ChannelAListBean();
            c.setBean(data);
            beans.add(c);
        }
        if (beans == null) {
            beans = new ArrayList<>();
            ChannelAListBean c = new ChannelAListBean();
            c.setBean(data);
            beans.add(c);
        }
        String json = "{\"channelAList\":" + JSON.toJSONString(beans) + "}";
        setChannelList(json);//存储
        /**同步文件缓存**/
        String request = GetFileUtil.getRequestChannelList(MyApp.currExploration);
        List<JChannelBean> jBeans = null;

        if (FastJsonUtil.JsonToCheck(request)) {
            jBeans = FastJsonUtil.JsonToGetRequestChannelAList(request);
        }
        if (jBeans == null) jBeans = new ArrayList<>();
        JChannelBean j = new JChannelBean();
        j.setChannel_id(data.getChannel_id());
        j.setUuid(data.getUUID());
        j.setExploration_id(MyApp.currExploration.getExploration_id().length() == 0 ? MyApp.currExploration.getUuid() : MyApp.currExploration.getExploration_id());
        j.setFeature_id(data.getExploration_id().length() == 0 ? data.getExploration_uuid() : data.getExploration_id());
        j.setDistance(data.getDistance());
        j.setPt_id(data.getPt_id());
        j.setFlag(MString.add);
        jBeans.add(j);
        setChannelRequestList("{\"channelAList\":" + JSON.toJSONString(jBeans) + "}");//存储同步文件
        //  MyApp.File.SaveFileData(CheckStageId_type(),MString.ExplorePointBelongFile+"_" +CheckExplorationId(),MString.ProjectExploreAcrossOnChannelList + "_" +CheckExplorationId(),data, "no");//存储
    }

    public static void updateChannelList(PhotoPassagewayItemBean data) {
        /**本地缓存**/
        MyLog.L(MString.SUCCESSED + " data==" + JSON.toJSONString(data));
        String s = GetFileUtil.getChannelList(MyApp.currExploration);
        List<ChannelAListBean> beans = null;
        if (FastJsonUtil.JsonToCheck(s)) {
            beans = FastJsonUtil.JsonToGetChannelAList(s);
            if (beans == null) return;
            for (ChannelAListBean bean : beans) {
                if (bean.getChannel_id().equals(data.getChannel_id()) && bean.getExploration_id().equals(data.getExploration_id())) {
                    bean.setDistance(data.getDistance());
                }
            }


        } else {

            return;
        }


        String json = "{\"channelAList\":" + JSON.toJSONString(beans) + "}";
        setChannelList(json);//存储
        /**同步文件缓存**/
        String request = GetFileUtil.getRequestChannelList(MyApp.currExploration);
        List<JChannelBean> jBeans = null;

        if (FastJsonUtil.JsonToCheck(request)) {
            jBeans = FastJsonUtil.JsonToGetRequestChannelAList(request);
        }
        if (jBeans == null) jBeans = new ArrayList<>();
        JChannelBean j = new JChannelBean();
        j.setChannel_id(data.getChannel_id());
        j.setUuid(data.getUUID());
        j.setExploration_id(MyApp.currExploration.getExploration_id().length() == 0 ? MyApp.currExploration.getUuid() : MyApp.currExploration.getExploration_id());
        j.setFeature_id(data.getExploration_id().length() == 0 ? data.getExploration_uuid() : data.getExploration_id());
        j.setDistance(data.getDistance());
        j.setPt_id(data.getPt_id());
        j.setFlag(MString.add);
        jBeans.add(j);
        setChannelRequestList("{\"channelAList\":" + JSON.toJSONString(jBeans) + "}");//存储同步文件
        //  MyApp.File.SaveFileData(CheckStageId_type(),MString.ExplorePointBelongFile+"_" +CheckExplorationId(),MString.ProjectExploreAcrossOnChannelList + "_" +CheckExplorationId(),data, "no");//存储
    }

    public static void delectChannelList(String id, PhotoPassagewayItemBean data) {
        /**本地缓存**/
        String s = GetFileUtil.getChannelList(MyApp.currExploration);
        List<ChannelAListBean> beans = FastJsonUtil.JsonToGetChannelAList(s);
        if (beans != null) {
            for (ChannelAListBean c : beans) {
                if (data.getChannel_id().equals(MString.code_null) && c.getExploration_id().length() > 0 && c.getExploration_id().equals(id)) {
                    beans.remove(c);
                    break;
                } else if (!data.getChannel_id().equals(MString.code_null) && c.getExploration_id().length() > 0 && c.getExploration_id().equals(id)) {
                    c.setFlag(MString.del);
                    break;
                }
            }
            String json = "{\"channelAList\":" + JSON.toJSONString(beans) + "}";
            setChannelList(json);//存储

        }
        //  MyApp.File.SaveFileData(CheckStageId_type(),MString.ExplorePointBelongFile+"_" +CheckExplorationId(),MString.ProjectExploreAcrossOnChannelList + "_" +CheckExplorationId(),data, "no");//存储
        /**同步文件缓存**/
        String request = GetFileUtil.getRequestChannelList(MyApp.currExploration);
        List<JChannelBean> jBeans = FastJsonUtil.JsonToGetRequestChannelAList(request);
        if (jBeans != null) {

            for (JChannelBean j : jBeans) {
                if (data.getChannel_id().equals(MString.code_null) && j.getFeature_id().length() > 0 && j.getFeature_id().equals(id)) {
                    jBeans.remove(j);
                    break;
                } else if (!data.getChannel_id().equals(MString.code_null) && j.getFeature_id().length() > 0 && j.getFeature_id().equals(id)) {
                    j.setFlag(MString.del);
                    break;
                }
            }
            setChannelRequestList("{\"channelAList\":" + JSON.toJSONString(jBeans) + "}");//同步文件删除
        }

    }

    /**
     * 保存项目踏勘点基本数据*
     */
    public static void setExplorationInfo(String data) {
        //保存基本点位信息
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExplorePointInfo + "_" + CheckExplorationId() + addEnd(), data, "n1");//存储
        //创建点位缓存文件夹
        MyApp.File.SaveFileData(MString.Project_ + CheckStageId_type(), MString.ExplorePointBelongFile + "_" + CheckExplorationId(), "1", "2", "3", "Voice");//存储
        MyApp.File.SaveFileData(MString.Request + File.separator + MString.Project_ + CheckStageId_type(), MString.ExplorePointBelongFile + "_" + CheckExplorationId(), "1", "2", "3", "Voice");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void setExplorationInfo(String data, String ExplorationId) {
        //保存基本点位信息
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectExplorePointInfo + "_" + ExplorationId + addEnd(), data, "n1");//存储
        //创建点位缓存文件夹
        MyApp.File.SaveFileData(MString.Project_ + CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId, "1", "2", "3", "Voice");//存储
        MyApp.File.SaveFileData(MString.Request + File.separator + MString.Project_ + CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId, "1", "2", "3", "Voice");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }


    /**
     * 删除踏勘点文件与文件夹*
     */
    public static void delectExploration(Exploration l) {
        String ExplorationId = MString.getValueId(l.getExploration_id(), l.getUuid());
        if (!(l.getExploration_id().equals(MString.code_null) || l.getExploration_id().equals(MString.code_zero))) {
            String s = GetFileUtil.getExplorationInfo(l);
            JSONObject jsonObject = JSONObject.parseObject(s);
            jsonObject.put(MString.flag, MString.del);
            setExplorationInfo(JSON.toJSONString(jsonObject), ExplorationId);
        } else {
            //保存基本点位信息
            //MyApp.File.SaveFileData(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId,"", "no");//存储
            MyApp.File.delect(CheckStageId_type(), MString.ProjectExplorePointInfo + "_" + ExplorationId + addEnd());
            MyApp.File.delectr(CheckStageId_type(), MString.ProjectExplorePointInfo + "_" + ExplorationId + addEnd());

            MyApp.File.delect(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId);
            MyApp.File.delectr(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId);
        }


    }

    public static void delectExploration(String Id, String uuid) {
//        try {

        String ExplorationId = MString.getValueId(Id, uuid);
        if (Id.equals(MString.code_null) || Id.equals(MString.code_zero)) {
            String s = GetFileUtil.getExplorationInfo(ExplorationId);
            JSONObject jsonObject = JSONObject.parseObject(s);
            if (jsonObject == null) jsonObject = new JSONObject();
            jsonObject.put(MString.flag, MString.del);
            setExplorationInfo(JSON.toJSONString(jsonObject), ExplorationId);
        } else {
            //保存基本点位信息
            //MyApp.File.SaveFileData(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId,"", "no");//存储
            MyApp.File.delect(CheckStageId_type(), MString.ProjectExplorePointInfo + "_" + ExplorationId + addEnd());
            MyApp.File.delectr(CheckStageId_type(), MString.ProjectExplorePointInfo + "_" + ExplorationId + addEnd());

            MyApp.File.delect(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId);
            MyApp.File.delectr(CheckStageId_type(), MString.ExplorePointBelongFile + "_" + ExplorationId);
        }

//        }catch (Exception e){}

    }

    /**
     * 基本项目信息*
     */
    public static void setProjectInfo(String data) {
        MyLog.L(MString.SUCCESSED + " sds;w存储：" + data);
        MyApp.File.SaveFileData(CheckStageId_type(), MString.ProjectStageBaseInfo + "_" + CheckStageId() + addEnd(), data, "no");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void setProjectInfo(String data, String stageid) {
        MyLog.L(MString.SUCCESSED + " sds;w存储：" + data);
        MyApp.File.SaveFileData(stageid, MString.ProjectStageBaseInfo + "_" + stageid + addEnd(), data, "no");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void setProjectInfoN1(String data, String stageid) {
        MyLog.L(MString.SUCCESSED + " sds;w存储：" + data);
        MyApp.File.SaveFileDataAll(stageid, MString.ProjectStageBaseInfo + "_" + stageid + addEnd(), data, "n1");//存储
        //DataFactory.setUpdate(true);//设置更新标识
    }

    public static void SaveFileRequestData(String data) {
        MyLog.L(MString.SUCCESSED + " save:______" + data);
        MyApp.File.SaveFileRequestData(MString.Request + File.separator + MString.Project_ + CheckStageId_type(), MString.ProjectStageBaseInfo + "_" + CheckStageId() + addEnd(), data, "no");//存储

    }

    public static void SaveFileRequestData(String data, String stageid) {
        MyLog.L(MString.SUCCESSED + " save:______" + data);
        MyApp.File.SaveFileRequestData(MString.Request + File.separator + MString.Project_ + stageid, MString.ProjectStageBaseInfo + "_" + stageid + addEnd(), data, "n1");//存储

    }


    private static boolean check(String json) {
        return json != null && json.length() > 0;
    }

    public static String CheckStageId() {
        String s = "";
        if (MyApp.currProject.getStage_id().equals(MString.code_null) || MyApp.currProject.getStage_id().equals(MString.code_zero)) {
            s = MyApp.currProject.getUUID();
        } else {
            s = MyApp.currProject.getStage_id();
        }
        return s;
    }

    public static String CheckStageId(ProjectItemBean bean) {
        String s = "";
        if (bean.getStage_id().equals(MString.code_null) || bean.getStage_id().equals(MString.code_zero)) {
            s = bean.getUUID();
        } else {
            s = bean.getStage_id();
        }
        return s;
    }

    public static String CheckStageId_type() {
        String s = "";
        if (MyApp.currProject.getStage_id().equals(MString.code_null) || MyApp.currProject.getStage_id().equals(MString.code_zero)) {
            s = MyApp.currProject.getUUID();
        } else {
            s = MyApp.currProject.getStage_id();
        }
        return s;
    }

    public static String CheckStageId_type(ProjectItemBean bean) {
        String s = "";
        if (bean.getStage_id().equals(MString.code_null) || bean.getStage_id().equals(MString.code_zero)) {
            s = bean.getUUID();
        } else {
            s = bean.getStage_id();
        }
        return s;
    }

    public static String CheckExplorationId() {
        String s = "";
        if (MyApp.currExploration.getExploration_id().equals(MString.code_null) || MyApp.currExploration.getExploration_id().equals(MString.code_zero)) {
            s = MyApp.currExploration.getUuid();
        } else {
            s = MyApp.currExploration.getExploration_id();
        }
        return s;
    }

    public static String addEnd() {
        return ".tio";
    }
}
